/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.controller.employee;

import com.common.json.EmployeeManagerJson;
import com.entity.Employee;
import com.model.EmployeeModel;
import java.io.Serializable;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

/**
 *
 * @author _HungChilvas_
 */
@Named(value = "addEmployee")
@RequestScoped
public class AddEmployee implements Serializable {
    private String username;
    private String password;
    private String email;
    private String fullname;
    private String gender;
    private String birthdate;
    private String phone;
    private String mobilephone;
    private String address;
    private String identityCard;
    private String position;
    private String departments;
    private String groups;
    private String avatar;
    /**
     * Creates a new instance of AddEmployee
     */
    public AddEmployee() {
    }
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getBirthdate() {
        return birthdate;
    }

    public void setBirthdate(String birthdate) {
        this.birthdate = birthdate;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getMobilephone() {
        return mobilephone;
    }

    public void setMobilephone(String mobilephone) {
        this.mobilephone = mobilephone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getIdentityCard() {
        return identityCard;
    }

    public void setIdentityCard(String identityCard) {
        this.identityCard = identityCard;
    }

    public String getPosition() {
        return position;
    }

    public void setPosition(String position) {
        this.position = position;
    }

    public String getDepartments() {
        return departments;
    }

    public void setDepartments(String departments) {
        this.departments = departments;
    }

    public String getGroups() {
        return groups;
    }

    public void setGroups(String groups) {
        this.groups = groups;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }
   
    private String getEmployeeJsonData(){
        Employee em = new Employee();
        boolean b = (username != null)&&(password != null)&&(avatar != null)&&(fullname != null)&&(phone != null)&&(mobilephone != null)
                &&(email != null)&&(address != null)&&(identityCard != null)&&(birthdate != null)&&(gender != null)&&(groups != null)
                &&(position != null)&&(departments != null);
        if(b){
            em.setUsername(username);
            em.setPassword(password);
            em.setAvatar(avatar);
            em.setFullname(fullname);
            em.setPhone(phone);
            em.setMobilephone(mobilephone);
            em.setEmail(email);
            em.setAddress(address);
            em.setIdentityCard(identityCard);
            String format = EmployeeManagerJson.getFormatDate(birthdate);
            em.setBirthdate(format);
            em.setGender(gender);
            em.setGroup(groups);
            em.setPosition(position);
            em.setDepartments(departments);
            em.setStatus("Working");
            em.setOnline("0");
            String currentDate = EmployeeManagerJson.getCurrentDatetime();
            em.setJoinInDate(currentDate);
            em.setLogged(currentDate);
            String json = EmployeeManagerJson.convertEmployeeInfoToJSONString(em);
            return json;
        }
        return null;
    }
    public String createEmplyee(){
        String emJson = getEmployeeJsonData();
        if(emJson != null){
            EmployeeModel model = new EmployeeModel();
            boolean result = model.createEmployee(emJson);
            if(result){
                return "employees?faces-redirect=true";
            }
        }
        return "error?faces-redirect=true";
    }
    
    
}
